package br.com.sgmf.web.managedbean;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import br.com.sgmf.enums.Estado;
import br.com.sgmf.enums.ModeloNF;
import br.com.sgmf.model.Fornecedor;
import br.com.sgmf.model.ItemNota;
import br.com.sgmf.model.NotaFiscal;
import br.com.sgmf.model.Peca;
import br.com.sgmf.service.FornecedorService;
import br.com.sgmf.service.NotaFiscalService;
import br.com.sgmf.service.PecaService;

@Component
@ManagedBean
@RequestScoped
public class NotaFiscalBean {

	private static final String BUSCAR_PECA = "Necessário buscar peça(s)";
	private static final String BUSCAR_FORNECEDOR = "Necessário buscar fornecedor";
	private static final String NOTA_FISCAL_SALVA_COM_SUCESSO = "Nota Fiscal salva com sucesso!";
	private static final String ERRO_AO_SALVAR_NOTA_FISCAL = "Erro ao salvar Nota Fiscal";

	private NotaFiscal notaFiscal;
	private Fornecedor fornecedor;
	private Peca peca;

	private List<Fornecedor> fornecedores;
	private List<Peca> pecas;
	private Set<ItemNota> itensNota;

	@Autowired
	private NotaFiscalService notaFiscalService;

	@Autowired
	private FornecedorService fornecedorService;

	@Autowired
	private PecaService pecaService;

	private String razaoSocial;
	private String nome;

	@PostConstruct
	public void inicializar() {
		notaFiscal = new NotaFiscal();
		peca = new Peca();
		itensNota = new HashSet<>();
	}

	public void novo() {
		notaFiscal = new NotaFiscal();
		fornecedor = null;
		peca = null;
		fornecedores = null;
		pecas = null;
		itensNota = new HashSet<>();
		razaoSocial = null;
		nome = null;
	}

	public String salvar() {
		FacesContext context = FacesContext.getCurrentInstance();
		FacesMessage message;
		if (fornecedor == null) {
			message = new FacesMessage(FacesMessage.SEVERITY_WARN,
					BUSCAR_FORNECEDOR, null);
		} else if (CollectionUtils.isEmpty(itensNota)) {
			message = new FacesMessage(FacesMessage.SEVERITY_WARN, BUSCAR_PECA,
					null);
		} else {
			try {
				notaFiscalService.save(notaFiscal, fornecedor, itensNota);
				message = new FacesMessage(FacesMessage.SEVERITY_INFO,
						NOTA_FISCAL_SALVA_COM_SUCESSO, null);
				inicializar();
			} catch (Exception e) {
				message = new FacesMessage(FacesMessage.SEVERITY_INFO,
						ERRO_AO_SALVAR_NOTA_FISCAL, null);
				e.printStackTrace();
			}
		}
		context.addMessage(null, message);
		return null;
	}

	public List<Fornecedor> buscarFornecedor() {
		fornecedores = fornecedorService.findByRazaoSocial(razaoSocial);
		razaoSocial = null;
		return fornecedores;
	}

	public void adicionarFornecedor(Fornecedor fornecedor) {
		this.fornecedor = fornecedor;
	}

	public List<Peca> buscarPeca() {
		pecas = pecaService.findByNome(nome);
		nome = null;
		return pecas;
	}

	public void adicionarPeca(Peca peca) {
		ItemNota itemNota = new ItemNota(1, peca);
		itensNota.add(itemNota);
	}

	public void incrementarItemNota(ItemNota itemNota) {
		itemNota.incrementAndGet();
	}

	public void decrementarItemNota(ItemNota itemNota) {
		if (itemNota.decrementAndGet() <= 0) {
			removerItemNota(itemNota);
		}
	}

	public void removerItemNota(ItemNota itemNota) {
		itensNota.remove(itemNota);
	}

	public ModeloNF[] getModelosNF() {
		return ModeloNF.values();
	}

	public Estado[] getEstados() {
		return Estado.values();
	}

	public NotaFiscal getNotaFiscal() {
		return notaFiscal;
	}

	public void setNotaFiscal(NotaFiscal notaFiscal) {
		this.notaFiscal = notaFiscal;
	}

	public Fornecedor getFornecedor() {
		return fornecedor;
	}

	public void setFornecedor(Fornecedor fornecedor) {
		this.fornecedor = fornecedor;
	}

	public Peca getPeca() {
		return peca;
	}

	public void setPeca(Peca peca) {
		this.peca = peca;
	}

	public List<Fornecedor> getFornecedores() {
		return fornecedores;
	}

	public void setFornecedores(List<Fornecedor> fornecedores) {
		this.fornecedores = fornecedores;
	}

	public List<Peca> getPecas() {
		return pecas;
	}

	public void setPecas(List<Peca> pecas) {
		this.pecas = pecas;
	}

	public Set<ItemNota> getItensNota() {
		return itensNota;
	}

	public void setItensNota(Set<ItemNota> itensNota) {
		this.itensNota = itensNota;
	}

	public String getRazaoSocial() {
		return razaoSocial;
	}

	public void setRazaoSocial(String razaoSocial) {
		this.razaoSocial = razaoSocial;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public NotaFiscalService getNotaFiscalService() {
		return notaFiscalService;
	}

	public void setNotaFiscalService(NotaFiscalService notaFiscalService) {
		this.notaFiscalService = notaFiscalService;
	}

	public FornecedorService getFornecedorService() {
		return fornecedorService;
	}

	public void setFornecedorService(FornecedorService fornecedorService) {
		this.fornecedorService = fornecedorService;
	}

	public PecaService getPecaService() {
		return pecaService;
	}

	public void setPecaService(PecaService pecaService) {
		this.pecaService = pecaService;
	}

}
